package com.bdth.bdthiss.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by Administrator on 2019/4/11.
 */
public class DateUtil {

    /**
     * 获取年月
     *
     * @return
     */
    public static String getYM(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        return sdf.format(date);
    }

    /**
     * 获取日
     *
     * @return
     */
    public static String getDay(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        return sdf.format(date);
    }

    /**
     * 获取上一天的日期
     *
     * @return
     */
    public static String getLastDate() {
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.DATE, -1);
        Date lastDate = ca.getTime(); //结果
        //求前一月ca.add(Calendar.MONTH, -1)，
        //前一天ca.add(Calendar.DATE, -1)
        //年份减1ca.add(Calendar.YEAR, -1)
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(lastDate);
    }

    /**
     * 获取上一天的日期
     *
     * @return
     */
    public static String getLastDate(String patter) {
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.DATE, -1);
        Date lastDate = ca.getTime(); //结果
        //求前一月ca.add(Calendar.MONTH, -1)，
        //前一天ca.add(Calendar.DATE, -1)
        //年份减1ca.add(Calendar.YEAR, -1)
        SimpleDateFormat sdf = new SimpleDateFormat(patter);
        return sdf.format(lastDate);
    }

    /**
     * 获取上N天的日期
     *
     * @param day 表示前N天
     * @return
     */
    public static String getLastDate(int day) {
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.DATE, -1 * day);
        Date lastDate = ca.getTime(); //结果
        //求前一月ca.add(Calendar.MONTH, -1)，
        //前一天ca.add(Calendar.DATE, -1)
        //年份减1ca.add(Calendar.YEAR, -1)
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(lastDate);
    }

    /**
     * 获取后N天的日期
     *
     * @param day 表示前N天
     * @return
     */
    public static String getDateLast(int day) {
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.DATE, day);
        Date lastDate = ca.getTime(); //结果
        //求前一月ca.add(Calendar.MONTH, -1)，
        //前一天ca.add(Calendar.DATE, -1)
        //年份减1ca.add(Calendar.YEAR, -1)
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(lastDate);
    }

    /**
     * 获取上N天的日期
     *
     * @param day 表示前N天
     * @return
     */
    public static String getLastDate(int day, String patter) {
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.DATE, -1 * day);
        Date lastDate = ca.getTime(); //结果
        //求前一月ca.add(Calendar.MONTH, -1)，
        //前一天ca.add(Calendar.DATE, -1)
        //年份减1ca.add(Calendar.YEAR, -1)
        SimpleDateFormat sdf = new SimpleDateFormat(patter);
        return sdf.format(lastDate);
    }

    public static String getRandomDateTime(String patter) {
        Random ran = new Random();
        int second = ran.nextInt(30);
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.SECOND, -1 * second);
        Date lastDate = ca.getTime(); //结果
        SimpleDateFormat sdf = new SimpleDateFormat(patter);
        return sdf.format(lastDate);
    }

    public static String format(Date date, String patter) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(patter);
            return sdf.format(date);
        } catch (Exception e) {
            return "-";
        }
    }

    public static String format(Date date) {
        return format(date, "yyyy-MM-dd");
    }

    public static String format(LocalDate date){
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(DATE_FORMAT_YYYY_MM_DD);
        return date.format(dtf);
    }

    public static Date toDate(String date, String patter) {
        SimpleDateFormat sdf = new SimpleDateFormat(patter);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    }

    public static Date toDate(String date) {
        return toDate(date, "yyyy-MM-dd");
    }

    /**
     * 两个时间差
     *
     * @param endDate
     * @param nowDate
     * @return
     */
    public static String getDatePoor(Date endDate, Date nowDate) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        long sec = diff % nd % nh % nm / ns;
        if (day == 0 && hour == 0 && min == 0) {
            return sec + "秒";
        }
        if (day == 0 && hour == 0) {
            return min + "分钟" + sec + "秒";
        }
        if (day == 0) {
            return hour + "小时" + min + "分钟" + sec + "秒";
        }
        return day + "天" + hour + "小时" + min + "分钟" + sec + "秒";
    }

    /**
     * 两个时间差
     * @return
     */
    public static String getDifTime(long diff) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        long sec = diff % nd % nh % nm / ns;
        if (day == 0 && hour == 0 && min == 0) {
            return sec + "秒";
        }
        if (day == 0 && hour == 0) {
            return min + "分钟" + sec + "秒";
        }
        if (day == 0) {
            return hour + "小时" + min + "分钟" + sec + "秒";
        }
        return day + "天" + hour + "小时" + min + "分钟" + sec + "秒";
    }

    public static void main(String[] args) {
//        // 距离JDK 14 发布还有多少天？
//        LocalDate jdk14 = LocalDate.of(2020, 3, 17);
//        LocalDate nowDate = LocalDate.now();
//        System.out.println("距离JDK 14 发布还有：" + nowDate.until(jdk14, ChronoUnit.DAYS) + "天");
//
//        LocalDateTime sj = LocalDateTime.of(2020, 3, 17 ,0 ,0,0);
//        LocalDateTime nowTime = LocalDateTime.now();
//        // 时间差
//        long day = nowTime.until(sj, ChronoUnit.DAYS);
//        long month = nowTime.until(sj, ChronoUnit.MONTHS);
//        long hours = nowTime.until(sj, ChronoUnit.HOURS);
//        long minutes = nowTime.until(sj, ChronoUnit.MINUTES);
//        System.out.println("相差月份" + month);
//        System.out.println("相差天数" + day);
//        System.out.println("相差小时" + hours);
//        System.out.println("相差分钟" + minutes);
//        System.out.println("相差："+ getDifTime(nowTime.until(sj, ChronoUnit.SECONDS) * 1000));

//        LocalDate startDate = getLocalDateByStr("2018-10-27");
//        LocalDate endDate = getLocalDateByStr("2018-11-05");
//        System.out.println(getLocalDateByDayStr(startDate,endDate));
//        List<String> strings = new ArrayList<>();
//        strings.add("31");
//        List<LocalDate> localDateListByweek = getLocalDateListByMonth(startDate, endDate, strings);
//        System.out.println(localDateListByweek);

//        String temp = String.format("insert into bdth_payment(ctcode,ctname,receive,crtime,opid,opname,type) " +
//                        "values('%s','%s',%.2f,'%s','%d','%s','%s')",
//                "1","jdjdj",120.987766,"2019-11-29",1,"bdthadmin","转账");
//        System.out.println(temp);
    }

    /**
     * 获取指定范围内所有日期，包含开始日期和结束日期
     * @return List<LocalDate>
     */
    public static List<LocalDate> getLocalDateByDay(LocalDate startDate, LocalDate endDate){
        List<LocalDate> localDateList = Stream.iterate(startDate, date -> date.plusDays(1)).
                limit(ChronoUnit.DAYS.between(startDate, endDate))
                .collect(Collectors.toList());
        localDateList.add(endDate);
        return localDateList;
    }

    /**
     * 获取指定范围内所有日期，包含开始日期和结束日期
     * @return List<LocalDate>
     */
    public static List<String> getLocalDateByDayStr(LocalDate startDate, LocalDate endDate){
        List<LocalDate> localDateList = Stream.iterate(startDate, date -> date.plusDays(1)).
                limit(ChronoUnit.DAYS.between(startDate, endDate))
                .collect(Collectors.toList());
        localDateList.add(endDate);
        return localDateList.stream().map(o->format(o).split("-")[2]).collect(Collectors.toList());
    }

    public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";


    public static LocalDate getLocalDateByStr(String str){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMAT_YYYY_MM_DD);
        return LocalDate.parse(str, formatter);
    }

    /**
     * 获取指定日期范围内的所有指定dayOfMonth 包含开始日期和结束日期
     * @param months 1,29,31表示每月的1号，29号，31号
     * @return List<LocalDate>
     */
    public static List<LocalDate> getLocalDateListByMonth(LocalDate startDate,LocalDate endDate,List<String> months){
        List<LocalDate> localDateList = new ArrayList<>();

        LocalDate localDate;
        for(String month : months){
            LocalDate tempDate = startDate;
            while (tempDate.isBefore(endDate) || tempDate.getMonthValue() == endDate.getMonthValue()){
                if(tempDate.lengthOfMonth() >= Integer.valueOf(month)){
                    localDate = tempDate.withDayOfMonth(Integer.valueOf(month));
                    if(localDate.isAfter(startDate) || localDate.isEqual(startDate)){
                        localDate = tempDate.withDayOfMonth(Integer.valueOf(month));
                        if(localDate.isEqual(endDate) || localDate.isBefore(endDate)){
                            localDateList.add(localDate);
                        }
                    }
                }
                tempDate = tempDate.plusMonths(1);
            }
        }
        return localDateList;
    }

}
